﻿using System;
using System.Text;
using System.Web.UI;
using SmartMenu.Classes;
using SmartMenu.Controllers;
using visinia.SmartEngine;
using visinia.SmartEngine.Modules;

public partial class Modules_General_SmartMenu_SmartMenu : WebModule
{
    protected override void WebModuleRender(HtmlTextWriter output)
    {
        int menuid = 0;
        if (Int32.TryParse((string)Settings["Menu"], out menuid))
        {
            MenuInfo menuInfo = MenuController.GetMenu(menuid);
            if (menuInfo != null)
            {
                string link = "<li><a href=\"{0}\">{1}</a></li>";
                string linkActive = "<li class=\"current\"><a href=\"{0}\">{1}</a></li>";
                string link_image = "<li><a href=\"{0}\"><img src=\"{1}\" />{2}</a></li>";
                string linkActive_image = "<li class=\"current\"><a href=\"{0}\"><img src=\"{1}\" style=\"margin-right: 20px; width: 16px; height: 16px\" />{2}</a></li>";

                StringBuilder sb = new StringBuilder();
                string rroot = WebRoots.GetRelativeWebRoot;
                string root = WebRoots.GetWebRoot;
                string currentUrl = Request.RawUrl;

                currentUrl = currentUrl.Remove(0, rroot.Length);
                if (currentUrl.EndsWith(".aspx"))
                    currentUrl = currentUrl.Remove(currentUrl.Length - 5);
                sb.Append("<ul>");
                foreach (MenuItemInfo menuItemInfo in MenuItemController.GetMenuItems(menuInfo.MenuId))
                {
                    if (!menuItemInfo.Visible)
                        continue;
                    string title = menuItemInfo.Title;
                    string url = string.Empty;
                    string image = string.Empty;

                    if (!string.IsNullOrEmpty(menuItemInfo.Image))
                    {
                        if (menuItemInfo.Image.StartsWith("externallink:", StringComparison.OrdinalIgnoreCase))
                            image = menuItemInfo.Image.Remove(0, 13);
                        else
                            image = root + menuItemInfo.Image;
                    }

                    if (menuItemInfo.Url.StartsWith("externallink:", StringComparison.OrdinalIgnoreCase))
                    {
                        url = menuItemInfo.Url.Remove(0, 13);
                        if (string.IsNullOrEmpty(image))
                            sb.AppendFormat(link, url, menuItemInfo.Title);
                        else
                            sb.AppendFormat(link_image, url, image, menuItemInfo.Title);
                    }
                    else
                    {
                        url = menuItemInfo.Url;
                        if (url.EndsWith(".aspx"))
                            url = url.Remove(url.Length - 5);
                        if (string.IsNullOrEmpty(image))
                        {
                            if (currentUrl.StartsWith(url, StringComparison.OrdinalIgnoreCase))
                                sb.AppendFormat(linkActive, root + url + ".aspx", title);
                            else
                                sb.AppendFormat(link, root + url + ".aspx", title);
                        }
                        else
                        {
                            if (currentUrl.StartsWith(url, StringComparison.OrdinalIgnoreCase))
                                sb.AppendFormat(linkActive_image, root + url + ".aspx", image, title);
                            else
                                sb.AppendFormat(link_image, root + url + ".aspx", image, title);
                        }
                    }
                }
                sb.Append("</ul>");
                output.Write(sb.ToString());
            }
        }
    }
}